﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

public partial class Encryption : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnEncrypt_Click(object sender, EventArgs e)
    {
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(drpWebSiteName.SelectedValue);
            ConfigurationSection machineKeySection = config.GetSection("system.web/machineKey");
            machineKeySection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
            machineKeySection.SectionInformation.ForceSave = true;
            config.Save();

            Response.Write("<h2 style='color:red'>Encryption Succeed</h2>");
        }
        catch(Exception ex)
        {
            Response.Write("<h2 style='color:red'>Error while encrypting</h2><br/>");
            Response.Write(ex.Message);
        }
    }
    protected void btnDecrypt_Click(object sender, EventArgs e)
    {
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(drpWebSiteName.SelectedValue);
            ConfigurationSection machineKeySection = config.GetSection("system.web/machineKey");
            machineKeySection.SectionInformation.UnprotectSection();
            machineKeySection.SectionInformation.ForceSave = true;

            config.Save();
            Response.Write("<h2 style='color:red'>Decryption Succeed</h2>");
        }
        catch (Exception ex)
        {
            Response.Write("<h2 style='color:red'>Error while decrypting</h2><br/>");
            Response.Write(ex.Message);
        }
    }
}
